// 注册的js
$(document).ready(function(){
    // 用户名
    var  user = document.getElementById("user");
    // 密码
    var pass = document.getElementById("pass");
    // 再次输入密码
    var pass2 = document.getElementById("pass2");
    // 电子邮箱
    var mail = document.getElementById("mail");
    // 手机号
    var tel = document.getElementById("tel");
    // 注册按钮
    var btn = document.getElementById("btn");
    // 勾选同意选项
    var  cheackbox = document.querySelector('.chk');

    var passOnOff = pass2OnOff = telOnOff = userOnoff = mailOnoff=cheackboxOnoff = btnOnoff=false;
    // 电话正则验证
    tel.onblur = function(){
        var reg = /^1[3-9]\d{9}$/; 
        if(reg.test(this.value)){
            this.nextElementSibling.innerHTML = "验证成功";
            telOnOff=true;

        }else{
            this.nextElementSibling.innerHTML = "请输入正确的手机号码";
            telOnOff = false;
        }
    }
    // 用户名正则验证
    user.onblur=function(){
        var reg =/^[a-zA-Z0-9_-]{4,16}$/;
        if(reg.test(this.value)){
            this.nextElementSibling.innerHTML = "合法";


        }else{
            this.nextElementSibling.innerHTML = "不合法";
        }
    }
    // 密码正则验证
    pass.oninput=function(){
        var a=b=c=0;
        // 专门用来判断长度的正则
        var lgReg = /^.{6,18}$/;
        if(!lgReg.test(this.value)){
            this.nextElementSibling.innerHTML = "安全风险高";
            passOnOff = false;
            return ;
        }
        // 判断是否出现数字
        var numReg=/\d/;
        if(numReg.test(this.value)){
            a = 1;
        }
         // 判断是否出现字母
         var azReg = /[a-z]/i;
         if(azReg.test(this.value)){
             b = 1;
         }
          // 判断是否实现非数字非字母
          var tsReg = /[^\da-z]/i;
          if(tsReg.test(this.value)){
              c = 1;
          }
          // 做最终的统计，决定难度等级
         switch(a+b+c){
            case 1:
                this.nextElementSibling.innerHTML = "简单";break;
            case 2:
                this.nextElementSibling.innerHTML = "一般";break;
            case 3:
                this.nextElementSibling.innerHTML = "困难";break;
        }
        passOnOff = true;
        // 重置密码的，要双向判断
        // 在第一个框，也要判断
        // 但是如果第二个框为空，可以暂时不判断
        if(pass2.value==="") return;
        if(this.value===pass2.value){
            pass2.nextElementSibling.innerHTML="一致";
            pass2OnOff=true;
        }
        else{
            pass2.nextElementSibling.innerHTML = "不一致";
            pass2OnOff = false;
        }
    }
pass2.oninput=function(){
    if(this.value=pass.value){
        if(this.value===pass.value){
            this.nextElementSibling.innerHTML="一致";
            pass2OnOff=true;
        }else{
            this.nextElementSibling.innerHTML="不一致";
            pass2OnOff=false;
        }
    }
    // 勾选同意框

    btn.onclick=function(){
        console.log(cheackbox.checked)
        if(cheackbox.checked&&telOnOff&&passOnOff&&pass2OnOff){
            alert("注册成功！欢迎尊贵的您加入!");
            // 数据添加
            let tel=$$("#tel").value;
            let pass=$$("#pass").value;

            if(!tel||!pass) return;

            // 拼接数据
            let param=`tel=${tel}&pass=${pass}`;

            // 发送请求
            ajax({url:'http://localhost/ylf/php/register.php?fn=add&' + param}).then(res=>{

                //
                res = JSON.parse(res);
                if(res.stateCode == 200){
        
                }
            })
            location.href="../page/sign.html";
            return;
        }
        if(!telOnOff){
            alert('手机号码不对')
    
        }
        if(!passOnOff){
            alert('密码不对')
        }
        if(!pass2OnOff){
            alert('重复密码不对')
        }
        if(!cheackbox.checked){
            alert('请勾选协议')
    
        }
    };
    // 把数据发送到php，然后发送到数据库

    // 获取节点
    function $$(ele){
        return document.querySelector(ele);
    }
    // 发送ajax 传送数据
    function ajax(param){
        return new Promise(function(resolve,reject){
            // ajax
            let xhr=new XMLHttpRequest();
            xhr.open("git",param.url);
            xhr.send();
            // 监听
            xhr.addEventListener("readystatechange",res);
            function res(){
                if(xhr.readyState == 4 && xhr.status == 200)resolve(xhr.response);
            }

        })
    }


}
})